<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*******************
***  从马帮导入指定SKU到新的系统
***  
*** 2017/07/18
**********/
class Formmabangsku
{
	
	function __construct()
	{    
		$this->CI =& get_instance();
        $this->db = $this->CI->load->database('default', TRUE);
		//$this->db2 = $this->CI->load->database('oracle', TRUE);
		$this->CI->load->model('Function_m','fun');
	}
	//$A 不带空格SKU，$notrimSKU mabangSKU
	//($parent_goods,$A sku,$B 仓位,$businessman,$businessmanline,$cosprice 成本)
	function getsaveParent($sku,$A,$B,$businessman="",$businessmanline="",$cosprice="",$mabangSKU='')
	{
		if($A=="")
		{
			return;
		}
		$mabangSKU = str_replace(",","",$mabangSKU);
		if($sku =='134875')
		{
			return;
		}
		$rsdb = $this->CI->fun->getdatabasesdetail('db_goods_parent',"AND p_parent_sku='".trim($sku)."'");
		if($rsdb)
		{
			$parentID =  $rsdb["p_parent_id"];
			$this->CI->load->library('Mabang');
			$MBrs = $this->CI->mabang->getskuname(trim($mabangSKU));
			
			if($MBrs !='200')
			{
				$rs = $MBrs[0];
			}else{
				return 0;
			}
			
			$goods_id = $this->getsavegoods($parentID,$A,$B,$rs,$businessman,$businessmanline,$cosprice);
		}else{
			//创建父SKU信息
			$this->CI->load->library('Mabang');
			$MBrs = $this->CI->mabang->getskuname(trim($mabangSKU));
			
			if($MBrs !='200')
			{
				$rs = $MBrs[0];
			}else{
				return 0;
			}
			if($rs->CATEGORYID==441)
			{
				$bigclassID = 1;
				$secondclassID = 6;
				$ThreeclassID = 15;
				$supplier = 3;
			}elseif($rs->CATEGORYID==444)
			{
				$bigclassID = 2;
				$secondclassID =4;
				$ThreeclassID = 0;
				$supplier = 2;
			}elseif($rs->CATEGORYID==443)
			{
				$bigclassID = 2;
				$secondclassID =4;
				$ThreeclassID = 0;
				$supplier = 1;
			}else
			{
				$bigclassID = 1;
				$secondclassID = 6;
				$ThreeclassID = 14;
				$supplier = 3;
			}
			if($rs->STATUS =='正常')
			{
				$status = 2;
			}
			if($rs->STATUS =='重点维护')
			{
				$status = 3;
			}
			if($rs->STATUS =='待开发')
			{
				$status = 8;
			}
			if($rs->STATUS =='清仓')
			{
				$status = 5;
			}
			if($rs->STATUS =='停产')
			{
				$status = 4;
			}
			if($rs->STATUS =='自动创建')
			{
				$status = 7;
			}
			if($rs->STATUS =='暂停销售')
			{
				$status =6;
			}
			if($rs->SELLPRICE==null)
			{
				$rs->SELLPRICE = 0;
			}
			if($rs->LENGTH==null)
			{
				$rs->LENGTH = 0;
			}
			if($rs->WIDTH==null)
			{
				$rs->WIDTH = 0;
			}
			if($rs->HIGH==null)
			{
				$rs->HIGH = 0;
			}
			
			if(is_numeric($goodsno) && preg_match('|^\d{8}$|',$goodsno))	//数字并且8位
			{
				if(substr($goodsno,0,2)==11)
				{
					$supplier = 1;	//11开头的8位数字 工厂1
				}
				if(substr($goodsno,0,2)==12)
				{
					$supplier = 2; //12开头的8位数字 工厂2
				}
			}else{
				$supplier = 3; //其他都是外采
			}
			$postdata="";
			if($rs->OPERTIME){
				$OPERTIME= date("Y-m-d", strtotime($rs->OPERTIME));
			}else{
				$OPERTIME="2016-01-01 23:59:59";
			}
			$postdata['p_parent_sku']	= trim($sku);
			$postdata['p_parent_name']	= $rs->NAME;
			$postdata['p_en_goods_name']= $rs->NAMEEN;
			$postdata['p_yz']	= 0;
			$postdata['p_bigclassid']	= $bigclassID;
			$postdata['p_secondclassid']= $secondclassID;
			$postdata['p_threeclassid'] = $ThreeclassID;
			$postdata['p_goods_brand'] = null;
			$postdata['p_costprice']	= $rs->COSTPRICE;
			$postdata['p_procurement']  = 15;
			$postdata['p_inventory_day']= 15;
			$postdata['p_weight']		= $rs->WEIGHT;
			$postdata['p_sellprice']	= $rs->SELLPRICE;
			$postdata['p_pd_length']	= $rs->LENGTH;
			$postdata['p_pd_width']		= $rs->WIDTH;
			$postdata['p_pd_height']	= $rs->HIGH;
			$postdata['p_pd_declare_value']= $rs->DECLAREDVALUE;
			$postdata['p_goodstypeID']	= 0;
			$postdata['p_sale_status']  = $status;
			$postdata['p_updatetime']	=$OPERTIME;
			$postdata['p_intime']	=time();
			$postdata['p_createID']	=$_COOKIE["userid"];
			$postdata['p_createUser']=$_COOKIE["username"];
			$postdata['p_supplier']=$supplier?$supplier:3;
			$postdata['p_checkman']	= 9999;
			$postdata['p_buyer']  = 9999;
			$postdata['standard']	= $rs->ALIAS3;
			$postdata['memo']  = $rs->DESCR;
			$this->db->query($this->db->insert_string('db_goods_parent', $postdata));
			$parentID = $this->db->insert_id();
			$goods_id = $this->getsavegoods($parentID,$A,$B,$rs,$businessman,$businessmanline,$cosprice);

		}
		
		
	
		return $goods_id;

	}
	

	function getNewsaveParent($sku,$post)
	{
		if($sku=="")
		{
			return;
		}
		$rsdb = $this->CI->fun->getdatabasesdetail('db_goods_parent',"AND p_parent_sku='".trim($sku)."'");
		if($rsdb)
		{
			$parentID =  $rsdb["p_parent_id"];
			$goods_id = $this->getNewsavegoods($parentID,$post);
		}else{
			//创建父SKU信息
		
			$bigclassID = 1;
			$secondclassID = 14;
			$ThreeclassID = 0;

			$goodsno = trim($sku);
			if(is_numeric($goodsno) && preg_match('|^\d{8}$|',$goodsno))	//数字并且8位
			{
				if(substr($goodsno,0,2)==11)
				{
					$supplier = 1;	//11开头的8位数字 工厂1
				}
				if(substr($goodsno,0,2)==12)
				{
					$supplier = 2; //12开头的8位数字 工厂2
				}
			}else{
				$supplier = 3; //其他都是外采
			}
			$status = 2;

			
			$postdata="";
			$OPERTIME= date("Y-m-d H:i:s");
			$postdata['p_parent_sku']	= trim($sku);
			$postdata['p_parent_name']	=addslashes($post['goods_name']);
			$postdata['p_en_goods_name']=addslashes($post['en_goods_name']);
			$postdata['p_yz']	= 0;
			$postdata['p_bigclassid']	=$post['bigclassID']?$post['bigclassID']:$bigclassID;
			$postdata['p_secondclassid']=$post['secondclassID']?$post['secondclassID']:$secondclassID;
			$postdata['p_threeclassid'] = $ThreeclassID;
			$postdata['p_goods_brand'] = null;
			$postdata['p_costprice']	= $post['g_costprice'];
			$postdata['p_procurement']  = $post['g_procurement']?$post['g_procurement']:7;
			$postdata['p_inventory_day']= $post['g_inventory_day']?$post['g_inventory_day']:7;
			$postdata['p_weight']		= $post['g_weight']?$post['g_weight']:400;
			$postdata['p_sellprice']	= $post['g_sellprice']?$post['g_sellprice']:0;
			$postdata['p_pd_length']	= 0;
			$postdata['p_pd_width']		= 0;
			$postdata['p_pd_height']	= 0;
			$postdata['p_pd_declare_value']= $post['g_pd_declare_value']?$post['g_pd_declare_value']:40;
			$postdata['p_goodstypeID']	= 0;
			$postdata['p_sale_status']  = $status;
			$postdata['p_updatetime']	=$OPERTIME;
			$postdata['p_intime']	=time();
			$postdata['p_createID']	=$_COOKIE["userid"];
			$postdata['p_createUser']=$_COOKIE["username"];
			$postdata['p_supplier']=$supplier?$supplier:3;
			
			$userlist =$this->CI->fun->getdatabaseslist('db_external_buy'," AND protector is NOT NULL and del_flag = 0 AND protector !='刘艺' ",'DISTINCT(protector)');
			foreach($userlist as $key=>$rs)
			{
				$yuanl[] = $rs['protector'];
			}


			if($post['g_checkman'] =='刘艺艺' || $post['g_checkman'] =='季骅骅' || $post['g_checkman'] =='谢容容' || in_array($post['g_checkman'],$yuanl) )
			{
				$postdata['p_checkman']	= $post['g_checkman']? $post['g_checkman']:'刘艺';
				$postdata['p_buyer']  = $post['g_checkman']? $post['g_checkman']:'刘艺';
			}else{

			$p_buyer = 9999;	//曾月
			$rUser = $this->CI->fun->getdatabaseslist('d_user'," AND `name` = '".$post['g_checkman']."'");
			if($rUser["classid"] == 3)	//德语销售部
			{
				$p_buyer = "7777";//姜
			}
			
			if($rUser["classid"] == 2 )	//欧美销售部
			{
				$p_buyer = "8888";//邢
			}
			if($rUser["id"] == 336 || $rUser["id"]==243)	//西班牙
			{
				$p_buyer = "7777";//姜
			}
			$arraycookie=array(368,107,119,175,219,282,334,335);
			if(in_array($rUser["id"],$arraycookie))	//西班牙
			{
				$p_buyer = "9999";//曾月
			}
			if( strpos($post['goods_name'],'鞋') !== false || strpos($post['goods_name'],'假发') !== false  )
			{
				$p_buyer = "6666";//高菲菲
			}


				$postdata['p_checkman']	= $p_buyer;
				$postdata['p_buyer']  = $p_buyer;
			}

				

			
			$postdata['standard']	= $post['standard'];
			$postdata['memo']  = $post['memo'];
			$this->db->query($this->db->insert_string('db_goods_parent', $postdata));
			$parentID = $this->db->insert_id();
			$goods_id = $this->getNewsavegoods($parentID,$post);

		}
		
		
	
		return $goods_id;
	}

	function getNewsavegoods($parentID,$post)
	{
		$skustr = $this->CI->fun->getdatabasesdetail('db_goods',"AND goods_sku='".trim($post['sku'])."'");
		if($skustr)
		{
			return $skustr["goods_id"];
		}
		$parentList = $this->CI->fun->getdatabasesdetail('db_goods_parent',"AND p_parent_id='".intval($parentID)."'");
		$postdata ="";
		$postdata["goods_parentID"] = $parentID;
		$postdata['goods_parent_sku']	= $parentList["p_parent_sku"];
		$postdata['goods_name']	= addslashes($post['goods_name']);
		$postdata['en_goods_name']	= addslashes($post['en_goods_name']);
		$postdata['yz']	= 0;
		$postdata['goods_sku']	=$post['sku'];
		$postdata['g_bigclassid']	= $parentList['p_bigclassid'];
		$postdata['g_secondclassid']=$parentList['p_secondclassid'];
		$postdata['g_threeclassid'] =$parentList['p_threeclassid'];
		$postdata['goods_brand']	=null;
		$postdata['g_costprice']	= $post["g_costprice"];
		$postdata['g_procurement']  = $post['g_procurement']?$post['g_procurement']:7;
		$postdata['g_inventory_day']=$post['g_inventory_day']?$post['g_inventory_day']:7;
		
		$postdata['g_weight']		=$post['g_weight']?$post['g_weight']:400;
		$postdata['g_sellprice']	=$post['g_sellprice']?$post['g_sellprice']:0;
		$postdata['g_pd_length']	=0;
		$postdata['g_pd_width']		=0;
		$postdata['g_pd_height']	=0;
		$postdata['g_pd_declare_value']= $post['g_pd_declare_value']?$post['g_pd_declare_value']:40;
		$postdata['g_goodstypeID']	= 0;
		$postdata['g_sale_status']  = $parentList['p_sale_status'];
		$postdata['g_updatetime']	=date("Y-m-d H:i:s");
		$postdata['g_checkman']	=  $post['g_checkman']? $post['g_checkman']:'刘艺';
		$postdata['g_buyer']  = $post['g_buyer']?$post['g_buyer']:'刘艺';
		$postdata['g_createID']		=$_COOKIE["userid"];
		$postdata['g_createUser']	=$_COOKIE["username"];
		$postdata['standard']	=  $post['standard'];
		$postdata['memo']  =  $post['memo'];
		$postdata['g_supplier']=$parentList['p_supplier'];
		
		if($post['cognatelist'])
		{
			$len = strlen($post['cognatelist']);
			if(substr($post['cognatelist'],$len-1) !=',')
			{
				$postdata['cognatelist']  = ",".trim($post['cognatelist']).",";
			}
		}


		$this->db->trans_begin();

		$this->db->query($this->db->insert_string('db_goods', $postdata));
		$goods_id = $this->db->insert_id();
	
		$imgdata = "";
		$imgdata["goods_id"] = $goods_id;
		$imgdata["updatetime"] = date("Y-m-d H:i:s");
		$imgdata["createUid"] = $_COOKIE["userid"];
		$imgdata["types"] = 1;
		$imgdata["content"] = 'Excel导入';
		$imgdata["createUser"] = $_COOKIE["username"];
		$this->db->query($this->db->insert_string('db_goods_log', $imgdata));

		//更新默认仓库
		
		$where = array('goods_id' => $goods_id);
		$rsdbstock = $this->db->get_where('db_goods_stock',$where)->result_array();
		if(!$rsdbstock){
			$imgdata = "";
			$imgdata["goods_id"] = $goods_id;
			$imgdata["storage_name"] = '一号仓';
			$imgdata["storage_id"] = 1;
			$imgdata["stock"] =0;
			$imgdata["in_transit"] = 0;
			$imgdata["positions"] ="";
			$this->db->query($this->db->insert_string('db_goods_stock', $imgdata));
		}
		
		//更新采购信息
		if($post['businessman'] && $post['businessmanline'])
		{
			$rsdbr =  $this->CI->fun->getdatabasesdetail("db_goods_purchasing"," AND goods_id='".$goods_id."'");
			if(!$rsdbr)
			{
				$postst="";
				$postst['goods_id'] = $goods_id;
				$postst['goods_sku'] =$post['sku'];
				$postst['parentID'] = $parentID;
				$postst['businessman'] = addslashes($post['businessman']);
				$postst['businessmanline'] = addslashes($post['businessmanline']);
				$this->db->insert("db_goods_purchasing",$postst);
			}
		}

		//处理图片
		$imagesUrl = $post['imagesUrl'];
		if($imagesUrl)
		{
			//先判断图片是否被下载过，如何下载过了直接共用一张图片，不需要再下载
				$rsdbr =  $this->CI->fun->getdatabasesdetail("db_goods_images"," AND images_url='".trim($imagesUrl)."'");
				if($rsdbr)
				{
						$postImages = "";
						$postImages["goods_id"] = $goods_id;
						$postImages["images_type"]  = 1;
						$postImages["filesize"]  = $rsdbr["filesize"];
						$postImages["images_url"]  = $rsdbr["images_url"];
						$postImages["local_url"]  = $rsdbr["local_url"];
						$postImages["thumb_url"]  =  $rsdbr["thumb_url"];
						$postImages["filename"]  = $rsdbr["filename"];
						$postImages["createUser"]  = $_COOKIE["username"];
						$postImages["instime"]  = time();
						$this->db->insert('db_goods_images',$postImages);
				
				}else{
					$this->CI->load->library('Mabang');
					$images2 = $this->CI->mabang->getMabangImages($imagesUrl, $parentList["p_parent_sku"]);
					if($images2){
						$postImages = "";
						$postImages["goods_id"] = $goods_id;
						$postImages["images_type"]  = 1;
						$postImages["filesize"]  = $images2["filesize"];
						$postImages["images_url"]  = $images2["images_url"];
						$postImages["local_url"]  = $images2["local_url"];
						$postImages["thumb_url"]  =  $images2["thumb_url"];
						$postImages["filename"]  = $images2["filename"];
						$postImages["createUser"]  = $_COOKIE["username"];
						$postImages["instime"]  = time();
						$this->db->insert('db_goods_images',$postImages);
					}
				}
		}

			if ($this->db->trans_status() === FALSE)
			{
				$this->db->trans_rollback();
			}
			else
			{
				$this->db->trans_commit();
			}
			return $goods_id;
	}
	function getsavegoods($parentID,$sku,$p,$rs,$businessman='',$businessmanline='',$cosprice='')
	{
		
		$skustr = $this->CI->fun->getdatabasesdetail('db_goods',"AND goods_sku='".trim($sku)."'");
		if($skustr)
		{
			return $skustr["goods_id"];
		}
		

		$parentList = $this->CI->fun->getdatabasesdetail('db_goods_parent',"AND p_parent_id='".intval($parentID)."'");
		$this->CI->load->library('Mabang');
		
		
		$postdata ="";
		$postdata["goods_parentID"] = $parentID;
		$postdata['goods_parent_sku']	= $parentList["p_parent_sku"];
		$postdata['goods_name']	= $rs->NAME;
		$postdata['yz']	= 0;
		$postdata['goods_sku']	= $sku;
		$postdata['g_bigclassid']	= $parentList['p_bigclassid'];
		$postdata['g_secondclassid']=$parentList['p_secondclassid'];
		$postdata['g_threeclassid'] =$parentList['p_threeclassid'];
		$postdata['en_goods_name']	= $rs->NAMEEN;
		$postdata['goods_brand']	=null;
		$postdata['g_costprice']	= $rs->COSTPRICE;
		$postdata['g_procurement']  = 15;
		$postdata['g_inventory_day']= 15;
		if($cosprice)
		{
			$postdata['g_costprice']= $cosprice;
		}
		$postdata['g_weight']		=$rs->WEIGHT;
		$postdata['g_sellprice']	=$rs->SELLPRICE?$rs->SELLPRICE:0;
		$postdata['g_pd_length']	=  $rs->LENGTH;
		$postdata['g_pd_width']		= $rs->WIDTH;
		$postdata['g_pd_height']	= $rs->HIGH;
		$postdata['g_pd_declare_value']= $rs->DECLAREDVALUE;
		$postdata['g_goodstypeID']	= 0;
		$postdata['g_sale_status']  = $parentList['p_sale_status'];
		$postdata['g_updatetime']	=date("Y-m-d H:i:s");
		$postdata['g_checkman']	= '刘艺';
		$postdata['g_buyer']  = '刘艺';
		$postdata['g_createID']		=$_COOKIE["userid"];
		$postdata['g_createUser']	=$_COOKIE["username"];
		$postdata['standard']	=  $rs->ALIAS3;
		$postdata['memo']  =  $rs->DESCR;
		$postdata['g_supplier']=$parentList['p_supplier'];
		if($rs->ALIAS1)
		{
			$postdata['cognatelist']= $rs->ALIAS1;
		}
		
		$this->db->query($this->db->insert_string('db_goods', $postdata));
		$goods_id = $this->db->insert_id();
	
		$imgdata = "";
		$imgdata["goods_id"] = $goods_id;
		$imgdata["updatetime"] = date("Y-m-d H:i:s");
		$imgdata["createUid"] = $_COOKIE["userid"];
		$imgdata["types"] = 1;
		$imgdata["content"] = '从马帮导入商品';
		$imgdata["createUser"] = $_COOKIE["username"];
		$this->db->query($this->db->insert_string('db_goods_log', $imgdata));


		//更新默认仓库
		
		$where = array('goods_id' => $goods_id);
		$rsdbstock = $this->db->get_where('db_goods_stock',$where)->result_array();
		if(!$rsdbstock){
			$imgdata = "";
			$imgdata["goods_id"] = $goods_id;
			$imgdata["storage_name"] = '一号仓';
			$imgdata["storage_id"] = 1;
			$imgdata["stock"] =0;
			$imgdata["in_transit"] = 0;
			$imgdata["positions"] =$p;
			$this->db->query($this->db->insert_string('db_goods_stock', $imgdata));
		}
		
		//更新采购信息
		if($businessman && $businessmanline)
		{
			$rsdbr =  $this->CI->fun->getdatabasesdetail("db_goods_purchasing"," AND goods_id='".$goods_id."'");
			if(!$rsdbr)
			{
				$post="";
				$post['goods_id'] = $goods_id;
				$post['goods_sku'] =$sku;
				$post['parentID'] = $parentID;
				$post['businessman'] = $businessman;
				$post['businessmanline'] = addslashes($businessmanline);
				$this->db->insert("db_goods_purchasing",$post);
			}
		}
		if($cosprice)
		{
			//$this->db->update("db_goods",array("g_costprice"=>$cosprice),"goods_parentID = '".$parentID."'");
		}
		return $goods_id;
	}


	
	/****保存开发期的商品信息 开始**************/
	function saveDevelopmentParent($sku,$post)
	{
		if($sku=='')
		{
			return;
		}
		
		$rsdb = $this->CI->fun->getdatabasesdetail('db_goods_parent',"AND p_parent_sku='".trim($sku)."'");//如果有主SKU了，那只保存其他的SKU信息做备用
		if($rsdb)
		{
			$parentID =  $rsdb["p_parent_id"];
			$goods_id = $this->getNewkaifaqsavegoods($parentID,$post);
		}else{
				$bigclassID = 1;
				$secondclassID = 14;
				$ThreeclassID = 0;
				$goodsno = trim($sku);
				if(is_numeric($goodsno) && preg_match('|^\d{8}$|',$goodsno))	//数字并且8位
				{
					if(substr($goodsno,0,2)==11)
					{
						$supplier = 1;	//11开头的8位数字 工厂1
					}
					if(substr($goodsno,0,2)==12)
					{
						$supplier = 2; //12开头的8位数字 工厂2
					}
				}else{
					$supplier = 3; //其他都是外采
				}
				$status = 2;
				$postdata="";
				$OPERTIME= date("Y-m-d H:i:s");
				$postdata['p_parent_sku']	= trim($sku);
				$postdata['p_parent_name']	=addslashes($post['goods_name']);
				$postdata['p_en_goods_name']=addslashes($post['en_goods_name']);
				$postdata['p_yz']	= 0;
				$postdata['p_bigclassid']	=$post['bigclassID']?$post['bigclassID']:$bigclassID;
				$postdata['p_secondclassid']=$post['secondclassID']?$post['secondclassID']:$secondclassID;
				$postdata['p_threeclassid'] = $ThreeclassID;
				$postdata['p_goods_brand'] = null;
				$postdata['p_costprice']	= $post['g_costprice'];
				$postdata['p_procurement']  = $post['g_procurement']?$post['g_procurement']:7;
				$postdata['p_inventory_day']= $post['g_inventory_day']?$post['g_inventory_day']:7;
				$postdata['p_weight']		= $post['g_weight']?$post['g_weight']:400;
				$postdata['p_sellprice']	= $post['g_sellprice']?$post['g_sellprice']:0;
				$postdata['p_pd_length']	= 0;
				$postdata['p_pd_width']		= 0;
				$postdata['p_pd_height']	= 0;
				$postdata['p_pd_declare_value']= $post['g_pd_declare_value']?$post['g_pd_declare_value']:40;
				$postdata['p_goodstypeID']	= 0;
				$postdata['p_sale_status']  = $status;
				$postdata['p_updatetime']	=$OPERTIME;
				$postdata['p_intime']	=time();
				$postdata['p_createID']	=$_COOKIE["userid"];
				$postdata['p_createUser']=$_COOKIE["username"];
				$postdata['p_supplier']=$supplier?$supplier:3;

				$userlist =$this->CI->fun->getdatabaseslist('db_external_buy'," AND protector is NOT NULL and del_flag = 0 AND protector !='刘艺' ",'DISTINCT(protector)');
				foreach($userlist as $key=>$rs)
				{
					$yuanl[] = $rs['protector'];
				}


				if($post['g_checkman'] =='刘艺艺' || $post['g_checkman'] =='季骅骅' || in_array($post['g_checkman'],$yuanl) )
				{
			
					$postdata['p_checkman']	= $post['g_checkman']? $post['g_checkman']:'刘艺';
					$postdata['p_buyer']  = $post['g_checkman']? $post['g_checkman']:'刘艺';
				}else{
					
					$p_buyer = 9999;	//曾月
					$rUser = $this->CI->fun->getdatabaseslist('d_user'," AND `name` = '".$post['g_checkman']."'");
					if($rUser["classid"] == 3)	//德语销售部
					{
						$p_buyer = "7777";//姜
					}
					if($rUser["classid"] == 2 )	//欧美销售部
					{
						$p_buyer = "8888";//邢
					}
					if($rUser["id"] == 336 || $rUser["id"]==243)	//西班牙
					{
						$p_buyer = "7777";//姜
					}
					$arraycookie=array(368,107,119,175,219,282,334,335);
					if(in_array($rUser["id"],$arraycookie))	//西班牙
					{
						$p_buyer = "9999";//曾月
					}
					if( strpos($post['goods_name'],'鞋') !== false || strpos($post['goods_name'],'假发') !== false  )
					{
						$p_buyer = "6666";//高菲菲
					}

					$postdata['p_checkman']	= $p_buyer;
					$postdata['p_buyer']  = $p_buyer;
				}
				$postdata['standard']	= $post['standard'];
				$postdata['memo']  = $post['memo'];
				$this->db->query($this->db->insert_string('db_goods_parent', $postdata));
				$parentID = $this->db->insert_id();
				$goods_id = $this->saveDevelopmentgoods($parentID,$post);

		}
		return $goods_id;
	}

	function saveDevelopmentgoods($parentID,$post)
	{
		$parentList = $this->CI->fun->getdatabasesdetail('db_goods_parent',"AND p_parent_id='".intval($parentID)."'");
		$skustr = $this->CI->fun->getdatabasesdetail('db_goods',"AND goods_parent_sku='".trim($parentList["p_parent_sku"])."'");
		if($skustr)
		{
			return $skustr["goods_id"];
		}
		
		$postdata ="";
		$postdata["goods_parentID"] = $parentID;
		$postdata['goods_parent_sku']	= $parentList["p_parent_sku"];
		$postdata['goods_name']	= addslashes($post['goods_name']);
		$postdata['en_goods_name']	= addslashes($post['en_goods_name']);
		$postdata['yz']	= 0;
		$postdata['goods_sku']	=$parentList['p_parent_sku'];
		$postdata['g_bigclassid']	= $parentList['p_bigclassid'];
		$postdata['g_secondclassid']=$parentList['p_secondclassid'];
		$postdata['g_threeclassid'] =$parentList['p_threeclassid'];
		$postdata['goods_brand']	=null;
		$postdata['g_costprice']	= $post["g_costprice"];
		$postdata['g_procurement']  = $post['g_procurement']?$post['g_procurement']:7;
		$postdata['g_inventory_day']=$post['g_inventory_day']?$post['g_inventory_day']:7;
		$postdata['g_weight']		=$post['g_weight']?$post['g_weight']:400;
		$postdata['g_sellprice']	=$post['g_sellprice']?$post['g_sellprice']:0;
		$postdata['g_pd_length']	=0;
		$postdata['g_pd_width']		=0;
		$postdata['g_pd_height']	=0;
		$postdata['g_pd_declare_value']= $post['g_pd_declare_value']?$post['g_pd_declare_value']:40;
		$postdata['g_goodstypeID']	= 0;
		$postdata['g_sale_status']  = $parentList['p_sale_status'];
		$postdata['g_updatetime']	=date("Y-m-d H:i:s");
		$postdata['g_checkman']	=  $post['g_checkman']? $post['g_checkman']:'刘艺';
		$postdata['g_buyer']  = $post['g_buyer']?$post['g_buyer']:'刘艺';
		$postdata['g_createID']		=$_COOKIE["userid"];
		$postdata['g_createUser']	=$_COOKIE["username"];
		$postdata['standard']	=  $post['standard'];
		$postdata['memo']  =  $post['memo'];
		$postdata['g_supplier']=$parentList['p_supplier'];
		
		if($post['sku'])
		{
			$len = strlen($post['sku']);
			if(substr($post['sku'],$len-1) !=',')
			{
				$postdata['cognatelist']  = ",".trim($post['sku']).",";
			}
		}


		$this->db->trans_begin();

		$goods_id = $this->getNewkaifaqsavegoods($parentID,$post);

		$this->db->query($this->db->insert_string('db_goods', $postdata));
		$goods_id = $this->db->insert_id();
	
		$imgdata = "";
		$imgdata["goods_id"] = $goods_id;
		$imgdata["updatetime"] = date("Y-m-d H:i:s");
		$imgdata["createUid"] = $_COOKIE["userid"];
		$imgdata["types"] = 1;
		$imgdata["content"] = 'Excel导入';
		$imgdata["createUser"] = $_COOKIE["username"];
		$this->db->query($this->db->insert_string('db_goods_log', $imgdata));

		//更新默认仓库
		
		$where = array('goods_id' => $goods_id);
		$rsdbstock = $this->db->get_where('db_goods_stock',$where)->result_array();
		if(!$rsdbstock){
			$imgdata = "";
			$imgdata["goods_id"] = $goods_id;
			$imgdata["storage_name"] = '一号仓';
			$imgdata["storage_id"] = 1;
			$imgdata["stock"] =0;
			$imgdata["in_transit"] = 0;
			$imgdata["positions"] ="";
			$this->db->query($this->db->insert_string('db_goods_stock', $imgdata));
		}
		
		//更新采购信息
		if($post['businessman'] && $post['businessmanline'])
		{
			$rsdbr =  $this->CI->fun->getdatabasesdetail("db_goods_purchasing"," AND goods_id='".$goods_id."'");
			if(!$rsdbr)
			{
				$postst="";
				$postst['goods_id'] = $goods_id;
				$postst['goods_sku'] =$post['sku'];
				$postst['parentID'] = $parentID;
				$postst['businessman'] = addslashes($post['businessman']);
				$postst['businessmanline'] = addslashes($post['businessmanline']);
				$this->db->insert("db_goods_purchasing",$postst);
			}
		}

		//处理图片
		$imagesUrl = trim($post['imagesUrl'],"\\");
		if($imagesUrl)
		{
			//先判断图片是否被下载过，如何下载过了直接共用一张图片，不需要再下载
				$rsdbr =  $this->CI->fun->getdatabasesdetail("db_goods_images"," AND images_url='".trim($imagesUrl)."'");
				if($rsdbr)
				{
						$postImages = "";
						$postImages["goods_id"] = $goods_id;
						$postImages["images_type"]  = 1;
						$postImages["filesize"]  = $rsdbr["filesize"];
						$postImages["images_url"]  = $rsdbr["images_url"];
						$postImages["local_url"]  = $rsdbr["local_url"];
						$postImages["thumb_url"]  =  $rsdbr["thumb_url"];
						$postImages["filename"]  = $rsdbr["filename"];
						$postImages["createUser"]  = $_COOKIE["username"];
						$postImages["instime"]  = time();
						$this->db->insert('db_goods_images',$postImages);
				
				}else{
					$this->CI->load->library('Mabang');
					$images2 = $this->CI->mabang->getMabangImages($imagesUrl, $parentList["p_parent_sku"]);
					if($images2){
						$postImages = "";
						$postImages["goods_id"] = $goods_id;
						$postImages["images_type"]  = 1;
						$postImages["filesize"]  = $images2["filesize"];
						$postImages["images_url"]  = $images2["images_url"];
						$postImages["local_url"]  = $images2["local_url"];
						$postImages["thumb_url"]  =  $images2["thumb_url"];
						$postImages["filename"]  = $images2["filename"];
						$postImages["createUser"]  = $_COOKIE["username"];
						$postImages["instime"]  = time();
						$this->db->insert('db_goods_images',$postImages);
					}
				}
		}

			if ($this->db->trans_status() === FALSE)
			{
				$this->db->trans_rollback();
			}
			else
			{
				$this->db->trans_commit();
			}
			return $goods_id;
	}

	function getNewkaifaqsavegoods($parentID,$post)
	{
		$parentList = $this->CI->fun->getdatabasesdetail('db_goods_parent',"AND p_parent_id='".intval($parentID)."'");
		$skustr = $this->CI->fun->getdatabasesdetail('db_goods',"AND goods_sku='".trim($post['sku'])."'");
		if($skustr)
		{
			return $skustr["goods_id"];
		}

		//如果遇到补充尺码的，继续补充
		$skustr = $this->CI->fun->getdatabasesdetail('db_goods',"AND goods_parentID='".intval($parentID)."' and goods_sku !='".trim($parentList['p_parent_sku'])."'");
		if($skustr)
		{
			$goods_id = $this->getNewsavegoods($parentID,$post);
			return $goods_id;
		}


		$skustr = $this->CI->fun->getdatabasesdetail('db_goods_reserved',"AND goods_sku='".trim($post['sku'])."'");
		if($skustr)
		{
			return $skustr["goods_id"];
		}
		
		
		$postdata ="";
		$postdata["goods_parentID"] = $parentID;
		$postdata['goods_parent_sku']	= $parentList["p_parent_sku"];
		$postdata['goods_name']	= addslashes($post['goods_name']);
		$postdata['en_goods_name']	= addslashes($post['en_goods_name']);
		$postdata['yz']	= 0;
		$postdata['goods_sku']	=$post['sku'];
		$postdata['g_bigclassid']	= $parentList['p_bigclassid'];
		$postdata['g_secondclassid']=$parentList['p_secondclassid'];
		$postdata['g_threeclassid'] =$parentList['p_threeclassid'];
		$postdata['goods_brand']	=null;
		$postdata['g_costprice']	= $post["g_costprice"];
		$postdata['g_procurement']  = $post['g_procurement']?$post['g_procurement']:7;
		$postdata['g_inventory_day']=$post['g_inventory_day']?$post['g_inventory_day']:7;
		
		$postdata['g_weight']		=$post['g_weight']?$post['g_weight']:400;
		$postdata['g_sellprice']	=$post['g_sellprice']?$post['g_sellprice']:0;
		$postdata['g_pd_length']	=0;
		$postdata['g_pd_width']		=0;
		$postdata['g_pd_height']	=0;
		$postdata['g_pd_declare_value']= $post['g_pd_declare_value']?$post['g_pd_declare_value']:40;
		$postdata['g_goodstypeID']	= 0;
		$postdata['g_sale_status']  = $parentList['p_sale_status'];
		$postdata['g_updatetime']	=date("Y-m-d H:i:s");
		$postdata['g_checkman']	=  $post['g_checkman']? $post['g_checkman']:'刘艺';
		$postdata['g_buyer']  = $post['g_buyer']?$post['g_buyer']:'刘艺';
		$postdata['g_createID']		=$_COOKIE["userid"];
		$postdata['g_createUser']	=$_COOKIE["username"];
		$postdata['standard']	=  $post['standard'];
		$postdata['memo']  =  $post['memo'];
		$postdata['g_supplier']=$parentList['p_supplier'];
		$postdata['imagesUrl']=$post['imagesUrl'];
		if($post['cognatelist'])
		{
			$len = strlen($post['cognatelist']);
			if(substr($post['cognatelist'],$len-1) !=',')
			{
				$postdata['cognatelist']  = ",".trim($post['cognatelist']).",";
			}
		}
		
		$this->db->query($this->db->insert_string('db_goods_reserved', $postdata));
		$goods_id = $this->db->insert_id();

		
		$skustr = $this->CI->fun->getdatabasesdetail('db_goods',"AND goods_parentID='".$parentID."'");//保存子SKU信息
		if($skustr){
			$cognatelist  = $skustr["cognatelist"].trim($post['sku']).",";
			$this->db->update("db_goods",array("cognatelist"=>$cognatelist),"goods_id='".$skustr['goods_id']."'");
		}
		
			return $goods_id;
	}
	/*******保存开发期商品结束******************/

}


?>